
//
//  SEGIAnalyticsNetWorkManager.m
//  test
//
//  Created by 刘山 on 2017/12/3.
//  Copyright © 2017年 刘山. All rights reserved.
//

#import "SEGIAnalyticsNetWorkManager.h"
#import "SEGIAnalyticsEventDBManager.h"
#import "SEGIAnalyticsLogErrorDBManager.h"

#if __has_include(<Commom/SEGHTTPBusinessManager.h>)
#import <Commom/SEGHTTPBusinessManager.h>
#else
#import "SEGHTTPBusinessManager.h"
#endif

@implementation SEGIAnalyticsNetWorkManager
/**
 * 上传统计数据  -事件
 * @author Sam
 */
+(void)segUploadAnalyticsEventData{
    NSArray *dataSource = [SEGIAnalyticsEventDBManager segEventQuryAll];
    if (!dataSource || dataSource.count <= 0)return;
    
    //改变数据上传格式
    NSMutableArray *data = [NSMutableArray array];
    [dataSource enumerateObjectsUsingBlock:^(NSDictionary*  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSMutableDictionary *dicM = [NSMutableDictionary dictionary];
        [dicM setObject:[obj valueForKey:@"pf"] forKey:@"pf"];
        NSMutableDictionary *temp = [NSMutableDictionary dictionaryWithDictionary:obj];
        [temp removeObjectForKey:@"pf"];
        [dicM setObject:temp forKey:@"evt"];
        [data addObject:dicM];
    }];
    
    NSString * baseString = [self base64EncodeArray:data];
    NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:baseString,@"val", nil];
    [SEGIAnalyticsEventDBManager segDeleteAll];
    
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/analysis-api/logs/logEvt",nil];
    [SEGHTTPBusinessManager seg_requestJSONWithMethod:POST URLString:urlString parameters:params completed:^(BOOL isSuccessed, id result, NSString *codeType, NSError *error) {
    }];
}

/**
 * 上传统计数据  -错误日志
 * @author Sam
 */
+(void)segUploadAnalyticsErrorData{
    NSArray *dataSource = [SEGIAnalyticsLogErrorDBManager segLogErrorQuryAll];
    if (!dataSource || dataSource.count <= 0) return;
    
    //改变数据上传格式
    NSMutableArray *data = [NSMutableArray array];
    [dataSource enumerateObjectsUsingBlock:^(NSDictionary*  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSMutableDictionary *dicM = [NSMutableDictionary dictionary];
        [dicM setObject:[obj valueForKey:@"pf"] forKey:@"pf"];
        NSMutableDictionary *temp = [NSMutableDictionary dictionaryWithDictionary:obj];
        [temp removeObjectForKey:@"pf"];
        [dicM setObject:temp forKey:@"evt"];
        [data addObject:dicM];
    }];
    
    NSString * baseString = [self base64EncodeArray:data];
    NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:baseString,@"val", nil];
    [SEGIAnalyticsLogErrorDBManager segDeleteAll];
    
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/analysis-api/logs/logErr",nil];
    [SEGHTTPBusinessManager seg_requestWithMethod:POST URLString:urlString parameters:params completed:^(BOOL isSuccessed, id result, NSString *codeType, NSError *error) {
    }];
}

+ (NSString *)base64EncodedString:(NSString*)content
{
    NSData *data = [content dataUsingEncoding:NSUTF8StringEncoding];
    return [data base64EncodedStringWithOptions:0];
}

+(NSString*)base64EncodeArray:(NSArray*)content{
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:content options:NSJSONWritingPrettyPrinted error:nil];
    return [jsonData base64EncodedStringWithOptions:0];
}

+ (NSString *)base64DecodedString:(NSString*)content
{
    NSData *data = [[NSData alloc]initWithBase64EncodedString:content options:0];
    return [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
}

@end
